package com.example.md.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.stereotype.Service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

@Service
public class UserService {
    
    /**
     * 获取 admin.json 
     * @return
     */
    public JSONObject getAdmin() throws IOException {
        File file = new File("admin.json");
        if (!file.exists()) {
            file.createNewFile();
            FileUtil.writeUtf8String("{}", file);
        }

        List<String> contents = FileUtil.readLines(file, "UTF-8");
        String jsonStr = StrUtil.join(System.lineSeparator(), contents);

        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        String username = jsonObject.getStr("username");
        String password = jsonObject.getStr("password");
        if (StrUtil.isBlank(password) || StrUtil.isBlank(username)) {
            return null;
        }

        return jsonObject;
    }
}
